
package com.benimetum.mail;

import com.sun.mail.smtp.SMTPTransport;
import java.io.*;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class MailSender implements Runnable
{
    private static final String username="";
    private static final String password="";
    private static final String host="";
    private int select;//0 = dosya yok 1= dosya var
    private String[] receiver;
    private String sender;
    private String subject;
    private String message;
    private File file;
    
    public MailSender(String[] receiver, String sender, String subject, String message) {
	  this.select = 0;
	  this.receiver=receiver;
	  this.sender=sender;
	  this.subject=subject;
	  this.message=message;
    }
    public MailSender(String[] receiver, String sender, String subject, String message, File file) {
	  this.select = 1;
	  this.receiver=receiver;
	  this.sender=sender;
	  this.subject=subject;
	  this.message=message;
	  this.file=file;
    }
    
    /**
     * tek bir alıcıya düz metin maili yollar
     * @param receiver
     * @param sender
     * @param subject
     * @param message
     * @return
     * @throws MessagingException
     * @throws IOException 
     */
    public static boolean sendMail(String receiver, String sender, String subject, String message) throws MessagingException, IOException{
	   Properties props = System.getProperties();
	   props.put("mail.smtp.host", host);
	   props.put("mail.transport.protocol", "smtp");
	   props.setProperty("mail.smtp.auth", "true");
	   props.put("mail.smtp.starttls.enable", "true");
	   props.setProperty("mail.smtp.ssl.trust", host);
	   props.setProperty("mail.smtp.auth.mechanisms", "LOGIN NTLM");
	   props.setProperty("mail.user", username);
	   props.setProperty("mail.password", password);
	   props.setProperty("mail.smtp.auth.ntlm.domain", "codomain");
	   props.put("mail.smtp.port", "587");
	    Session session = Session.getInstance(props);
	    Message msg = new MimeMessage(session);
	    if (sender != null)
		msg.setFrom(new InternetAddress(sender));
	    else
		msg.setFrom();

	    msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(receiver));
	    msg.setSubject(subject);
	    InputStream is=new ByteArrayInputStream(message.getBytes());
	    String text = collect(new BufferedReader(new InputStreamReader(is)));
		msg.setText(text);
	    msg.setHeader("X-Mailer", "BenimEtum");
	    msg.setSentDate(new Date());

	    Transport t = session.getTransport("smtp");
	    try {
		t.connect(host,587, username, password);
		t.sendMessage(msg, msg.getAllRecipients());
	    }catch(Exception e){
		return false;
	    } finally {
		System.out.println("Done");
		t.close();
	    }
	    return true;
    }
      
    /**
     * birden fazla alıcıya mail atar
     * @param receiver
     * @param sender
     * @param subject
     * @param message
     * @return
     * @throws MessagingException
     * @throws IOException 
     */
    public static boolean sendMail(String[] receiver, String sender, String subject, String message) throws MessagingException, IOException{
	   Properties props = System.getProperties();
	   props.put("mail.smtp.host", host);
	   props.put("mail.transport.protocol", "smtp");
	   props.setProperty("mail.smtp.auth", "true");
	   props.put("mail.smtp.starttls.enable", "true");
	   props.setProperty("mail.smtp.ssl.trust", host);
	   props.setProperty("mail.smtp.auth.mechanisms", "LOGIN NTLM");
	   props.setProperty("mail.user", username);
	   props.setProperty("mail.password", password);
	   props.setProperty("mail.smtp.auth.ntlm.domain", "codomain");
	   props.put("mail.smtp.port", "587");
	    Session session = Session.getInstance(props);
	    Message msg = new MimeMessage(session);
	    if (sender != null)
		msg.setFrom(new InternetAddress(sender));
	    else
		msg.setFrom();
	    for (int i = 0; i < receiver.length; i++) {
		msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(receiver[i]));
		
	  }
	    msg.setSubject(subject);
	    InputStream is=new ByteArrayInputStream(message.getBytes());
	    String text = collect(new BufferedReader(new InputStreamReader(is)));
		msg.setText(text);
	    msg.setHeader("X-Mailer", "BenimEtum");
	    msg.setSentDate(new Date());

	    Transport t = session.getTransport("smtp");
	    try {
		t.connect(host,587, username, password);
		t.sendMessage(msg, msg.getAllRecipients());
	    }catch(Exception e){
		return false;
	    } finally {
		System.out.println("Done");
		t.close();
	    }
	    return true;
    }
    
    /**
     * birden fazla alıcıya ek bulunan mail atar
     * @param receiver
     * @param sender
     * @param subject
     * @param message
     * @param file
     * @return
     * @throws MessagingException
     * @throws IOException 
     */
    public static boolean sendMail(String[] receiver, String sender, String subject, String message, File file) throws MessagingException, IOException{
	   Properties props = System.getProperties();
	   props.put("mail.smtp.host", host);
	   props.put("mail.transport.protocol", "smtp");
	   props.setProperty("mail.smtp.auth", "true");
	   props.put("mail.smtp.starttls.enable", "true");
	   props.setProperty("mail.smtp.ssl.trust", host);
	   props.setProperty("mail.smtp.auth.mechanisms", "LOGIN NTLM");
	   props.setProperty("mail.user", username);
	   props.setProperty("mail.password", password);
	   props.setProperty("mail.smtp.auth.ntlm.domain", "codomain");
	   props.put("mail.smtp.port", "587");
	    Session session = Session.getInstance(props);
	    Message msg = new MimeMessage(session);
	    if (sender != null)
		msg.setFrom(new InternetAddress(sender));
	    else
		msg.setFrom();
	    for (int i = 0; i < receiver.length; i++) {
		msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(receiver[i]));
		
	    }
	    msg.setSubject(subject);
	    InputStream is=new ByteArrayInputStream(message.getBytes());
	    String text = collect(new BufferedReader(new InputStreamReader(is)));
	    MimeBodyPart mbp1 = new MimeBodyPart();
	    mbp1.setText(text);
	    MimeBodyPart mbp2 = new MimeBodyPart();
	    mbp2.attachFile(file);
	    MimeMultipart mp = new MimeMultipart();
	    mp.addBodyPart(mbp1);
	    mp.addBodyPart(mbp2);
	    msg.setContent(mp);
	    msg.setHeader("X-Mailer", "BenimEtum");
	    msg.setSentDate(new Date());

	    Transport t = session.getTransport("smtp");
	    try {
		t.connect(host,587, username, password);
		t.sendMessage(msg, msg.getAllRecipients());
	    }catch(Exception e){
		return false;
	    } finally {
		System.out.println("Done");
		t.close();
	    }
	    return true;
    }
    
    /**
     * tek alıcıya içinde ek bulunan mail yollar
     * @param receiver
     * @param sender
     * @param subject
     * @param message
     * @param file
     * @return
     * @throws MessagingException
     * @throws IOException 
     */
    public static boolean sendMail(String receiver, String sender, String subject, String message, File file) throws MessagingException, IOException{
	   Properties props = System.getProperties();
	   props.put("mail.smtp.host", host);
	   props.put("mail.transport.protocol", "smtp");
	   props.setProperty("mail.smtp.auth", "true");
	   props.put("mail.smtp.starttls.enable", "true");
	   props.setProperty("mail.smtp.ssl.trust", host);
	   props.setProperty("mail.smtp.auth.mechanisms", "LOGIN NTLM");
	   props.setProperty("mail.user", username);
	   props.setProperty("mail.password", password);
	   props.setProperty("mail.smtp.auth.ntlm.domain", "codomain");
	   props.put("mail.smtp.port", "587");
	    Session session = Session.getInstance(props);
	    Message msg = new MimeMessage(session);
	    if (sender != null)
		msg.setFrom(new InternetAddress(sender));
	    else
		msg.setFrom();
		msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(receiver));
	    msg.setSubject(subject);
	    InputStream is=new ByteArrayInputStream(message.getBytes());
	    String text = collect(new BufferedReader(new InputStreamReader(is)));
	    MimeBodyPart mbp1 = new MimeBodyPart();
	    mbp1.setText(text);
	    MimeBodyPart mbp2 = new MimeBodyPart();
	    mbp2.attachFile(file);
	    MimeMultipart mp = new MimeMultipart();
	    mp.addBodyPart(mbp1);
	    mp.addBodyPart(mbp2);
	    msg.setContent(mp);
	    msg.setHeader("X-Mailer", "BenimEtum");
	    msg.setSentDate(new Date());

	    Transport t = session.getTransport("smtp");
	    try {
		t.connect(host,587, username, password);
		t.sendMessage(msg, msg.getAllRecipients());
	    }catch(Exception e){
		return false;
	    } finally {
		System.out.println("Done");
		t.close();
	    }
	    return true;
    }
    
    private static String collect(BufferedReader in) throws IOException {
	String line;
	StringBuffer sb = new StringBuffer();
	while ((line = in.readLine()) != null) {
	    sb.append(line);
	    sb.append("\n");
	}
	return sb.toString();
    }

    @Override
    public void run() {
	  if(select==0)
		try {
		sendMail(receiver, sender, subject, message);
	  } catch (MessagingException ex) {
		Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
	  } catch (IOException ex) {
		Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
	  }
	  else if(select==1)
		try {
		sendMail(receiver, sender, subject, message, file);
	  } catch (MessagingException ex) {
		Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
	  } catch (IOException ex) {
		Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
}
